#include <QDomDocument>

#include "coveragemodelxmladapter.h"

///////// Class 'CoverageModelXMLAdapter' implementation ///////////////////////////////////////////

CoverageModelXMLAdapter::CoverageModelXMLAdapter(QObject * parent)
    : QObject(parent)
{
}

QString CoverageModelXMLAdapter::name() const
{
    return "coveragemodelxmladapter";
}

QString CoverageModelXMLAdapter::description() const
{
    return tr("This plugin provides storing workflow coverage information to XMl files.");
}

bool CoverageModelXMLAdapter::saveModel(CoverageModel * model, QIODevice * dest)
{
    if(!dest->isWritable())
        return false;

    WorkflowCoverageMap map = model->coverageMap();
    QDomDocument document("coverage");

    foreach(ARSWorkflowObject * object, map.keys())
    {
        QDomElement objectElement = document.createElement("object");
        objectElement.setAttribute("name", object->name());
        objectElement.setAttribute("type", QString::number(object->type()));

        foreach(const DynamicModel * dynamicModel, map.values())
        {
            QDomElement modelElement = document.createElement("model");
            modelElement.setAttribute("name", dynamicModel->sourceName());
            objectElement.appendChild(modelElement);
        }

        document.appendChild(objectElement);
    }

    dest->write(document.toString().toAscii());

    return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
